package models

import (
	"github.com/astaxie/beego/orm"
)

type ConfigModels struct {
}

type Config struct {
	Id       int    `orm:pk;size(8)`
	Name     string `orm:size(50);default("")`
	Value    string `orm:size(512);default("")`
	Inc_type string `orm:size(64);default("")`
	Desc     string `orm:size(50);default("")`
}

func init() {
	orm.RegisterModel(new(Config))
}

//获取指定名称的对象
func (m *ConfigModels) Get(name string) (Config, error) {
	config := Config{}
	ps := getQueryTable("config")
	err := ps.Filter("name", name).One(&config)
	return config, err
}

//所有列表
func (m *ConfigModels) Query(maps map[string]interface{}) ([]Config, error) {
	var list []Config
	ps := getQueryTable("config")
	if maps["inc_type"] != nil {
		ps = ps.Filter("inc_type", maps["inc_type"])
	}
	_, err := ps.All(&list)
	return list, err
}

//添加修改设置
func (m *ConfigModels) Update(maps map[string]interface{}) (int64, error) {
	if len(maps["value"].(string)) <= 0 {
		return 0, nil
	}
	config := Config{}
	config.Name = maps["name"].(string)
	config.Value = maps["value"].(string)
	if maps["inc_type"] != nil {
		config.Inc_type = maps["inc_type"].(string)
	}
	if maps["desc"] != nil {
		config.Inc_type = maps["desc"].(string)
	}
	ps := getQueryTable("config")
	if ps.Filter("name", config.Name).Exist() {
		return ps.Filter("name", config.Name).Update(maps)
	} else {
		o := orm.NewOrm()
		return o.Insert(&config)
	}
}

//删除
func (m *ConfigModels) Delete(id int) (int64, error) {
	o := orm.NewOrm()
	return o.Delete(&Config{Id: id})
}
